import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../../../../presenter/state_setting_presenter.dart';
import '../info_page.dart';

class SettingPage extends StatefulWidget {
  const SettingPage(
      {Key? key, required this.presenter, required this.controller})
      : super(key: key);
  final StateSettingPresenter presenter;
  final SettingController controller;

  static const routeName = '${InfoPage.routeName}/setting';
  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  final _formKey = GlobalKey<FormState>();
  @override
  void initState() {
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(I18n.setting),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          children: [
            TextFormField(
              decoration: const InputDecoration(
                border: UnderlineInputBorder(),
                labelText: I18n.ip,
              ),
              initialValue: widget.presenter.client.host,
              validator: (value) {
                if (value == null ||
                    value.isEmpty ||
                    !RegExp(I18n.ip_regex).hasMatch(value)) {
                  return I18n.invalid_ip;
                }
                widget.presenter.host = value;
                return null;
              },
            ),
            TextFormField(
              keyboardType: TextInputType.visiblePassword,
              decoration: const InputDecoration(
                border: UnderlineInputBorder(),
                labelText: I18n.port,
              ),
              initialValue: widget.presenter.port.toString(),
              validator: (value) {
                if (value == null ||
                    value.isEmpty ||
                    !RegExp(I18n.port_regex).hasMatch(value)) {
                  return I18n.invalid_port;
                }
                widget.presenter.port = int.parse(value);
                return null;
              },
            ),
            ElevatedButton(
              onPressed: () {
                if (_formKey.currentState!.validate()) {
                  setState(() {
                    widget.presenter.connectionValid = null;
                  });
                  SharedPreferences.getInstance()
                      .then((instance) => instance.clear());
                  widget.presenter.client
                      .setHostEnv(widget.presenter.host, widget.presenter.port);
                  final token = widget.presenter.client.authorization;
                  widget.presenter.client.setAuthorization("");
                  widget.controller.validateClient(token);
                }
              },
              child: const Text(I18n.apply),
            ),
            Text('${I18n.ip}: ${widget.presenter.client.host}'),
            Text('${I18n.port}: ${widget.presenter.client.port.toString()}'),
            Text(
                '${I18n.connection_valid}: ${widget.presenter.connectionValid}'),
            Text(
                '${I18n.token}: ${widget.presenter.client.authorization.isEmpty ? null.toString() : widget.presenter.client.authorization}'),
          ],
        ),
      ),
    );
  }
}
